const mongoose = require("mongoose");
const authorModel = require("./authorModel");
let BookSchema = new mongoose.Schema({
  name: String,
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Author",
  },
  price: Number,
});
BookSchema.pre("remove", async function (next) {
  try {
    const author = await authorModel.deleteOne({ _id: this.author.toString()});
    console.log({ _id: this.author.toString()});
    console.log(
      'author=>',author
    );
    next();
  } catch (err) {
    next(err);
  }
});
//创建模型对象  对文档操作的封装对象
let BookModel = mongoose.model("books", BookSchema);
//暴露模型对象
module.exports = BookModel;
